package com.jinlong.controller;

import com.jinlong.base.controller.BaseController;
import com.jinlong.constants.BaseApiConstants;
import com.jinlong.constants.Constants;
import com.jinlong.entity.UserEntity;
import com.jinlong.feign.UserFeign;
import com.jinlong.web.CookieUtil;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.oauth.Oauth;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController extends BaseController{

    private static final String LOGIN = "login";
    private static final String INDEX = "index";
    private static final String ERROR = "error";
    private static final String ASSOCIATEDACCOUNT = "associatedAccount";

    @Autowired
    private UserFeign userFeign;

    @RequestMapping("/locaLogin")
    public String locaLogin(String source,HttpServletRequest request){
        request.setAttribute("source",source);
        return LOGIN;
    }

    @RequestMapping("/login")
    public String login(UserEntity userEntity, String source, HttpServletRequest request,
                        HttpSession httpSession, HttpServletResponse response){
            if(!StringUtils.isEmpty(source)&&source.equals(Constants.USER_SOURCE_QQ)){
               String openid = (String) httpSession.getAttribute(Constants.USER_SESSION_OPENID);
               userEntity.setOpenid(openid);
            }

            Map<String, Object> loginMap = userFeign.login(userEntity);
            Integer code = (Integer)loginMap.get(BaseApiConstants.HTTP_CODE_NAME);
            if(!code.equals(BaseApiConstants.HTTP_200_CODE)){
                String msg = (String) loginMap.get("msg");
                return setError(request,msg,LOGIN);
            }

        String token = (String) loginMap.get("data");
        CookieUtil.addCookie(response, Constants.USER_TOKEN,token,Constants.WEBUSER_COOKIE_TOKEN_TERMVALIDITY);
            return INDEX;
    }
    @RequestMapping("/authorizerUrl")
    public String authorizerUrl(HttpServletRequest request) throws QQConnectException {

        String authorizeURL = new Oauth().getAuthorizeURL(request);
        return "redirect"+authorizeURL;
    }

    @RequestMapping("/qqLoginCallback")
    public String qqLoginCallback(HttpServletRequest request,HttpSession httpSession,HttpServletResponse response){
        try{
            AccessToken accessTokenObj = new Oauth().getAccessTokenByRequest(request);
            if(accessTokenObj == null){
                return setError(request,"qq授权失败",ERROR);
            }
            String accessToken = accessTokenObj.getAccessToken();
            if(StringUtils.isEmpty(accessToken)){
                return setError(request,"qq授权失败",ERROR);
            }
            //获取openid
            OpenID openidObj = new OpenID(accessToken);
            String userOpenID = openidObj.getUserOpenID();
            //使用userOpenID查询信息看看用户是否绑定
            Map<String, Object> openIdMap = userFeign.findLogin(userOpenID);
            Integer code = (Integer) openIdMap.get(BaseApiConstants.HTTP_CODE_NAME);
            if(code.equals(BaseApiConstants.HTTP_200_CODE)){
                // 已经授权过,自动登录
                String token = (String) openIdMap.get("data");
                CookieUtil.addCookie(response, Constants.USER_TOKEN, token,
                        Constants.WEBUSER_COOKIE_TOKEN_TERMVALIDITY);
                return INDEX;
            }
            // 没有绑定openid
            httpSession.setAttribute("openid", userOpenID);
        }catch (Exception e){

        }

        // 跳转到关联页面
        return ASSOCIATEDACCOUNT;
    }

}
